<template>
    <div @click="handClick" :class="select.isHit ? 'title2' : 'title'" :style="myStyles">
        <div class="pd" :class="select.isSelect ? 'pd2' : ''" v-show="!select.isHit">{{ select.nowSelectName }}</div>
        <div class="pd3" v-show="select.isHit">{{ select.nowSelectName }}</div>
        <img class="imgRow pd" src="../../assets/images/upRow.svg">
        <div class="popup" v-if="show && !disabled" :style="myStyles">
            <div v-for="s in dataList" v-bind:key="s">
                <div class="item_name" @click="selectItem(s)" v-if="s.dictValue != '-1' || isOmit">
                    <div v-if="s.dictName === select.nowSelectName" class="item_name_default">{{ s.dictName }}</div>
                    <div v-else>{{ s.dictName }}</div>
                </div>
            </div>
        </div>
    </div>

    <div class="overlay" v-if="show" @click="handClick"></div>
</template>
    
<script>

export default {
    name: 'CommonSelectKey',
    emits: ['returnData', 'clearData', 'mustCount'],
    components: {
    },
    props: {
        dataList: Array,
        default: String,
        isOmit: Boolean,
        disabled: Boolean,
        myStyles: {
            default: { width: '156px' }
        },
        isMust: {
            default: false,
            type: Boolean
        },
        mustHit: {
            default: '',
            type: String
        },
        insertKey: String,
        isSelect: {
            default: false
        },
        clear: Boolean
    },
    data() {
        return {
            show: false,
            select: {
                isSelect: this.isSelect,
                nowSelectName: this.default,
                value: '',
                isHit: false
            }
        }
    },
    mounted() {
    },
    methods: {
        handClick() {
            this.show = !this.show
            if (this.show)
                this.$nextTick(() => {
                    const popupEl = document.querySelector('.popup');

                    // 如果找到了弹出框元素
                    if (popupEl) {
                        // 计算弹出框底部距离窗口底部的距离
                        const bottomDist = popupEl.getBoundingClientRect().bottom - window.innerHeight;
                        // 如果底部距离小于弹出框高度，说明弹出框被遮挡，需要向上移动
                        if (bottomDist > 30) {
                            // 修改弹出框的样式，将其向上移动
                            popupEl.style.top = `-250px`;
                        } else {
                            // 如果不被遮挡，清除样式，恢复原位置
                            popupEl.style.top = 'none';
                        }
                    }
                })
        },
        selectItem(s) {
            this.select = {
                isSelect: true,
                nowSelectName: s.dictName,
                value: s.dictValue,
                isHit: false
            }
            this.show = !this.show
        },
        inputReturn() {
            let flag = true
            if (this.isMust) {
                if (this.select.nowSelectName == this.default && !this.isSelect) {
                    this.select.nowSelectName = this.mustHit
                    this.select.isHit = true
                    flag = false
                }
                if (this.select.nowSelectName == this.mustHit) {
                    this.select.isHit = true
                    flag = false
                }
            }
            return flag
        },
    },
    watch: {
        "select.value": {
            handler() {
                let temp = { [this.insertKey]: this.select.value }
                this.show = !this.show
                return this.$emit('returnData', temp)
            }
        },
        clear() {
            this.select.nowSelectName = this.mustHit
            this.select.isSelect = false
        },
        default() {
            this.select.nowSelectName = this.default
        },
        isSelect() {
            this.select.isSelect = this.select
        }
    },
    computed: {
    }

}
</script>
    
<style scoped>
.popup-top {
    top: 0;
}

.df {
    display: flex;
}

.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: transparent;
}

.title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: white;
    border: 1px solid #e1e1e1;
    font-size: 12px;
    width: 120px;
    border-radius: 2px;
    padding-top: 4px;
    padding-bottom: 4px;
    color: rgb(153, 153, 153);
    margin: 1px;
    position: relative;
    height: 14px;
}

.title2 {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: white;
    border: 1px solid red;
    font-size: 12px;
    width: 120px;
    border-radius: 2px;
    padding-top: 4px;
    padding-bottom: 4px;
    color: rgb(153, 153, 153);
    margin: 1px;
    position: relative;
    height: 14px;
}

.pd {
    padding-left: 10px;
    padding-right: 3px;
    white-space: nowrap;
    overflow: hidden;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    -webkit-box-orient: vertical;
}

.pd2 {
    color: black;
}

.pd3 {
    padding-left: 15px;
    padding-right: 10px;
    color: red;
    white-space: nowrap;
    overflow: hidden;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    -webkit-box-orient: vertical;
}

.pd3::before {
    content: '*';
    color: red;
    position: absolute;
    left: 9px;
    margin-top: 2px;
}

.popup {
    position: absolute;
    background: white;
    border: 1px solid #eee;
    box-shadow: 1px 1px 15px #e5e5e5;
    z-index: 1;
    font-size: 13px;
    margin-top: 1px;
    font-size: 12px;
    top: 23px;
    max-height: 250px;
    overflow: auto;
}

.imgRow {
    width: 10px;
    height: 10px;
    -webkit-transform: rotate(180deg);
    -moz-transform: rotate(180deg);
    -ms-transform: rotate(180deg);
    -o-transform: rotate(180deg);
    transform: rotate(180deg);
    padding-left: 5px;
}

.item_name {
    text-align: left;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    color: black;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 5px;
}

.item_name:hover {
    background-color: #eee;
}

.item_name_default {
    color: #6698ff;
}
</style>